package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "分类管理相关接口")
@Slf4j
@RequestMapping("/admin/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @PutMapping
    @ApiOperation("修改分类")
    public Result updateCategorytype(@RequestBody CategoryDTO categoryDTO){
        categoryService.updateCategorytype(categoryDTO);
        return Result.success();

    }

    /**
     * 分页查询
     * @param categoryPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分类菜品分页查询")
    public Result<PageResult> page(CategoryPageQueryDTO categoryPageQueryDTO){
        log.info("分类分页查询");
        PageResult pageResult= categoryService.page(categoryPageQueryDTO);
        return Result.success(pageResult);
    }


    /**
     * 启用禁用菜品
     * @param
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("启用禁用菜品")
    public Result startOrStop(@PathVariable Integer status, Long id){
        log.info("启用禁用菜品:{},{}",status,id);
        categoryService.startOrStop(status,id);
        return Result.success();
    }

@PostMapping
@ApiOperation("新增分类")
    public Result save(@RequestBody CategoryDTO categoryDTO){
        log.info("新增菜品分类:{}",categoryDTO);
        categoryService.save(categoryDTO);
        return Result.success();
    }

  @DeleteMapping
  @ApiOperation("根据id删除分类菜品")
    public Result delete(Long id){
        log.info("本次删除的菜品是:{}",id);
        categoryService.deleteById(id);
        return Result.success();
    }


    @GetMapping("/list")
    @ApiOperation("根据状态查询分类")
    /**
     * 根据类型查询分类
     */
    public Result<List<Category>> list(Integer type){
  List<Category> category= categoryService.getByType(type);
    return Result.success(category);
    }
}
